import csv
import os

# 获取当前文件所在目录的绝对路径 拼接一个项目根目录
project_path = os.path.join(os.path.dirname(__file__), '../')


def read_token():
    """读取config/token.txt"""
    # 拼接文件路径
    file_path = os.path.join(project_path, 'config/token.txt')
    # 打开文件 以读取的方式, 打开的文件起别名f
    with open(file_path, 'r', encoding='UTF-8') as f:
        return f.read()


def read_csv_to_dict(filename):
    name = os.path.join(project_path, f'testdata/{filename}')
    with open(name, 'r', encoding='UTF-8') as f:
        reader = csv.DictReader(f)  # 把每一行读成一个字典,key取第一行
        data = []
        for row in reader:
            data.append(row) # 把每个字典写入一个list中
        return data


def read_csv_to_list(filename):
    name = os.path.join(project_path, f'testdata/{filename}')
    with open(name, 'r', encoding='UTF-8') as f:
        reader = csv.reader(f)  # 把每一行读成一个列表
        data = []
        for row in reader:
            data.append(row)
        data.pop(0)  # 删除表头
        return data
